/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.academy.web.action.subject;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ar.uba.fi.posgrado.academy.model.AcademicRecord;
import ar.uba.fi.posgrado.academy.model.Coursing;
import ar.uba.fi.posgrado.academy.service.AcademicRecordService;
import ar.uba.fi.posgrado.academy.service.SubjectService;
import ar.uba.fi.posgrado.academy.service.TeacherService;
import ar.uba.fi.posgrado.common.service.StudentService;
import ar.uba.fi.posgrado.common.util.CommonUtils;

public class CreateAcademicRecordAction extends Action {
	/** Logger */
	private static Logger logger = Logger
			.getLogger(CreateAcademicRecordAction.class);

	private AcademicRecordService academicRecordService;

	private TeacherService teacherService;

	private SubjectService subjectService;

	private StudentService studentService;

	public void setAcademicRecordService(
			AcademicRecordService academicRecordService) {
		this.academicRecordService = academicRecordService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public void setSubjectService(SubjectService subjectService) {
		this.subjectService = subjectService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	/**
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		if (logger.isDebugEnabled())
			logger.debug("Por crear un acta ...");

		AcademicRecord academicRecord = new AcademicRecord();
		academicRecord.setTeacher(teacherService.getById(Long.parseLong(request
				.getParameter("teacherId"))));
		academicRecord.setSubject(subjectService.getById(Long.parseLong(request
				.getParameter("subjectId"))));
		academicRecord.setDate(CommonUtils.toDateFromStringStandard(request
				.getParameter("date")));

		Set<Coursing> coursings = new HashSet<Coursing>();
		String coursingsStr = request.getParameter("coursingsStr");
		if (!coursingsStr.trim().equals(""))
			for (String coursingStr : coursingsStr.split(";")) {
				Coursing coursing = new Coursing();
				String[] coursingArray = coursingStr.split(",", 2);
				coursing.setAcademicRecord(academicRecord);
				coursing.setStudent(studentService.getById(Long
						.parseLong(coursingArray[0])));
				coursing.setMark(Byte.parseByte(coursingArray[1]));
				coursings.add(coursing);
			}
		academicRecord.setCoursings(coursings);

		this.academicRecordService.create(academicRecord);

		logger.info("Acta " + academicRecord + " creada con exito");

		return mapping.findForward("success");
	}
}
